<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if($_RQ['type']=='work'){
		$this->model('model')->checkvip();
	}
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','id desc');
	set_html('title',$return['pluginset']['title']?'首页-'.$return['pluginset']['title']:'虚拟币首页');
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title'],
		'desc'=>$return['pluginset']['share_desc'],
		'link'=>custom_url('jy_creditchange/index/',true),
		'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['share_image'],
	);
	$return['params']=array('page'=>1,'keyword'=>'');
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);	
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
		}else{
			$params['realname like']='%'.$_RQ['keyword'].'%';
		}
		$return['params']['keyword']=$_RQ['keyword'];		
	}
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_creditchange_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');	
	//按排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['image']=tomedia($item['image']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$item['usernum']=pdo_count('jy_creditchange_order',array('goodsid'=>$item['id'],'ispay'=>1),'id');
			$item['creditname']=$this->credit_types[$item['type']];
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='post'){
		$post=get_postdata();
		if(!$post['fee']){
			exi('请输入竞拍价！','error');
		}
		$item=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
		$orders=$this->model('model')->checkorders($post['goodsid']);
		if($orders[0]['fee']>=$post['fee']){
			exi('当前出价不能低于自己的最高出价！','error');
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_creditchange_order','ordersn'),
			'fee'=>$post['fee'],
			'goodsid'=>$post['goodsid'],
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_creditchange_order',$insert);
		$order_id=pdo_insertid();
		$params=array(
			'id'=>$order_id,
			'title'=>'虚拟币交易竞价',
			'fee'=>$post['fee'],
			'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_creditchange/index/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_creditchange/order/',
			'error_url'=>'jy_creditchange/order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_creditchange_order',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
		);
		exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_creditchange/order/');
	}
	$order=pdo_get('jy_creditchange_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_creditchange/order/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_weishop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
		'status'=>1,
		'ispay'=>1,
	);
	pdo_update('jy_creditchange_order',$params,array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
	exi('提交成功!');
}